/*
package org.scolring.eclipse.scol.editor.rules;
*/

package editorwords.editors;

import org.eclipse.jface.util.Assert;
import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;

/**
 * RÃ¨gle dÃ©tectant les nombres hexadÃ©cimaux (de la forme 0x1234567890ABCDEF).  
 * @author SÃ©bastien Le Ray
 */
public class TeteWordRule implements IRule
{

		private IToken fToken;
		private static final String mantisChars = " _.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijlkmnopqrstuvwxyz";
		private static final String numberChars = "0123456789";		
		
		/**
		 * Constructeur.
		 * La rÃ¨gle renverra token si lors de l'analyse
		 * elle trouve une chaine hexadÃ©cimale. 
		 * @param token Token Ã  renvoyer en cas de succÃ¨s.
		 */
		public TeteWordRule(IToken token) {
			Assert.isNotNull(token);
			fToken = token;
		}
		
		/**
		 * Indique si le caractÃ¨re passÃ© en paramÃ¨tre est un caractÃ¨re
		 * hexadÃ©cimal valide.
		 * @param c CaractÃ¨re Ã  analyser.
		 * @return <code>true</code> si le caractÃ¨re est un caractÃ¨re
		 * hexadÃ©cimal, <code>false</code> sinon.
		 */
		private boolean isHexDigit(int c) {
			return (mantisChars.indexOf(c) >= 0);
		}

		private boolean isNumberDigit(int c) {
			return (numberChars.indexOf(c) >= 0);
		}		
		
		private int readAndCount(ICharacterScanner scanner, CharPosition charPosition) {
			charPosition.counter++ ;
			charPosition.car = scanner.read() ;
			System.out.print("/" + charPosition.car);
			return charPosition.car ;
		}
		
		
		/**
		 * DÃ©termine si l'on est en prÃ©sence d'une chaine
		 * correspondant Ã  un nombre hexadÃ©cimal ou non.
		 * AppelÃ© par l'SCOLCodeScanner (via le RuleBasedScanner).
		 * @param scanner Scanner permettant de lire le texte.
		 * @return Le token passÃ© au constructeur si la chaine est une chaine hexa.
		 * <code>Token.UNDEFINED</code> sinon ce qui permet de continuer l'analyse.
		 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
		 */
		public IToken evaluate(ICharacterScanner scanner) 
		{
			CharPosition charPosition = new CharPosition() ;
			int c  ;
			boolean nbOK = false ;
			boolean isPb = false ;
			int countNumber ;
			System.out.println("launch scan");
			c = readAndCount(scanner, charPosition) ;
			
			if (c == 'm') {
				System.out.println("m trouve ");
				countNumber = 0 ;
				c = readAndCount(scanner, charPosition) ;
				while ( isNumberDigit((char) c) ) {
					countNumber++ ;
					System.out.print(c);
					c = readAndCount(scanner, charPosition) ;
				}
				if (countNumber >= 3) {
					nbOK = true ;
					System.out.print("nb ok");
				}
				System.out.print(" <" + c + ">");
				//isPb = true ;
				
				
				if (  c == ' ') {
					isPb = true ;
					System.out.print("pb");
				}
				
				
				/*
				do {
					c = readAndCount(scanner, charPosition) ;
					
				} while(isHexDigit((char) c));
				 */

				if (nbOK & isPb) {
					do {
						c = readAndCount(scanner, charPosition) ;
						if(c == 10 || c == 13 || c == -1 || c == '\t' ) { //13=saut ligne, -1=EOF
						scanner.unread();
						System.out.println(" end trouve ");
						return fToken;	
						}
					} while(isHexDigit((char) c));
				}
				
			}
			
			
			for (int i = 0 ; i < charPosition.counter ; i++) {
				scanner.unread();
				System.out.println("unread" );
			}
			System.out.println(" end nothing ");
			return Token.UNDEFINED;
						
		}
		


}